//
//  AbnormalViewController.m
//  OrderFood
//
//  Created by xiaofang wang on 2018/10/23.
//  Copyright © 2018年 zhiyuan. All rights reserved.
//  @"异常上报"

#import "AbnormalViewController.h"
#import "AbnomalDetailViewController.h"
#import "ErrorCell.h"
#import "FooterView.h"
#import "HeadderView.h"
@interface AbnormalViewController ()<UITableViewDataSource,UITableViewDelegate,ErrorCellDelegate>
@property (strong, nonatomic)NSMutableArray <OrderModel *> *dataSource;

@end

@implementation AbnormalViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    _dataSource = [NSMutableArray new];
    [self loadTableViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-SafeAreaTopHeight-TabBar_HEIGHT-MHTitilesViewH - SafeAreaBottomHeight) tableViewStyle:UITableViewStyleGrouped refreshStyle:LSRefreshTypeJustHeader];
    self.mytableview.delegate = self;
    self.mytableview.dataSource = self;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self ls_headerBeginRefreshing];
    
    NSLog(@"来了");
}

- (void)requestNewData{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self netWorkError];
    });
}
//- (void)requestMoreData{
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [self ls_footerEndRefreshing];
//    });
//
//}

-(void)netWorkError {
    
    WeakObj(self)
    [NetWork requestExceptionReportListSuccessBlock:^(NSDictionary * _Nonnull response) {
        if (RES_SUCESS) {
            [weakself.dataSource removeAllObjects];
            for (NSDictionary *dic in [response objectForKey:@"data"]) {
                OrderModel *orderModel = [OrderModel new];
                [orderModel yy_modelSetWithJSON:dic];
                orderModel.orderId = [NSString stringWithFormat:@"%@", [dic objectForKey:@"id"]];
                [weakself.dataSource addObject:orderModel];
            }
            [weakself.mytableview reloadData];
        }else {
            [HUD showMessage:MESSAGE];
        }
        [self ls_headerEndRefreshing];
    } failBlock:^(NSString * _Nonnull message) {
        [HUD showMessage:FAIL_MESSAGE];
        [self ls_headerEndRefreshing];
    }];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"ErrorCellID";
    ErrorCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[ErrorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.orderModel = [_dataSource objectAtIndex:indexPath.section];
    cell.deleateE = self;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AbnomalDetailViewController *detailVC = [[AbnomalDetailViewController alloc] init];
    detailVC.hidesBottomBarWhenPushed =YES;
    detailVC.orderModel = [_dataSource objectAtIndex:indexPath.section];
    [self.navigationController pushViewController:detailVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00000001;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}


-(void)errorCellDidCall:(ErrorCell *)cell isUser:(BOOL)isUser {
    NSIndexPath *indexPath = [self.mytableview indexPathForCell:cell];
    if (isUser) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"detail_callUser", nil) message:[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"detail_call", nil),_dataSource[indexPath.section].user.phone] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"detail_cancel",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"detail_sure",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertVC addAction:cancelAction];
        [alertVC addAction:sureAction];
        [self presentViewController:alertVC animated:YES completion:nil];
    }else {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"detail_callMerchant", nil) message:[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"detail_call", nil),_dataSource[indexPath.section].shop.phone] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"detail_cancel",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"detail_sure",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertVC addAction:cancelAction];
        [alertVC addAction:sureAction];
        [self presentViewController:alertVC animated:YES completion:nil];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
